Page({
  data: {
    menuList: [],
    sorting: [],
    goodsList: 0,
    cids: "",
    sortKind: "",
    paixu: true,
    goodsStart: 1,
    rows: 20,
    end:999,
    scrollTop:1,
    endFlage:1,
    sku:"",
    addflage:1
  },
  onShow: function () {
    getApp().getCartNum(this);
  },
  onLoad:function(e){
   
    if (e.categoryid == undefined){
      e.categoryid = '';
    }
    console.log(e.categoryid)
    this.setData({
      categoryid: e.categoryid
    })
    this.setSorting();
    this.getMenuList();
  },
  setSorting() {
    var sortList = [
      { title: "综合排序", select: 0, kind: "comprehensive" },
      { title: "按价格", select: 1, kind: "vipPrice"},
      { title: "按销量", select: 1, kind: "prodcutsellamount" }
    ];
    this.setData({
      sorting: sortList,
      sortKind: sortList[0].kind
    })
  },
  getMenuList() {
    var url = getApp().data.url + 'solr/product/getCategory';
    var body = {};
    getApp().wxAjax(this.splitMenu, url, body);
  },
  splitMenu(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      var menuObj = resObj.body.category_list[0].sub_list;
      var myMenObjArr = [];
      for (let i = 0; i < menuObj.length; i++) {
        var myMenuObj = {};
        myMenuObj.select = 1;
        if (i == 0) {
          if (this.data.categoryid != '') {
            myMenuObj.select = 1;
          } else {
            myMenuObj.select = 0;
          }

          this.setData({
            cids: menuObj[i].sub_category_id
          })
        }
      

        myMenuObj.sub_category_id = menuObj[i].sub_category_id;
        myMenuObj.sub_category_image = menuObj[i].sub_category_image;
        myMenuObj.sub_category_name = menuObj[i].sub_category_name;
        if (this.data.categoryid != '') {
          if (this.data.categoryid == myMenuObj.sub_category_id){
            myMenuObj.select = 0;
          }
          this.setData({
            cids: this.data.categoryid
          })
        }
        myMenObjArr.push(myMenuObj);
      }
      console.log(myMenObjArr)
      this.setData({
        menuList: myMenObjArr
      })
      this.getGoodsListParameter();
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  tapStor(e) {
    var index = e.currentTarget.dataset.index;
    var sorting = this.data.sorting;
    for (let i = 0; i < sorting.length; i++) {
      sorting[i].select = 1;
      if (index == i) {
        sorting[i].select = 0;
        this.setData({
          sortKind: sorting[i].kind
        })
      }
    }
    this.setData({
      goodsStart: 1,
      endFlage: 1,
      end: 999,
      sorting: sorting
    })
    console.log(this.data.sortKind)
    if (this.data.sortKind == 'vipPrice') {
      if (this.data.paixu) {
        this.setData({
          paixu: false
        })
      } else {
        this.setData({
          paixu: true
        })
      }
    } else {
      this.setData({
        paixu: true
      })
    }
    console.log(this.data.sortKind)
    this.goTopFun();
    this.getGoodsListParameter();
  },
  goTopFun: function (e) {
    var _top = this.data.scrollTop;
    if (_top == 1) {
      _top = 0;
    } else {
      _top = 1;
    }
    this.setData({
      'scrollTop': _top
    });
    console.log("----");
    console.log(this.data.scrollTop)
  },
  tapMenu(e) {
    var index = e.currentTarget.dataset.index;
    var menuList = this.data.menuList;
    for (let i = 0; i < menuList.length; i++) {
      menuList[i].select = 1;
      if (index == i) {
        menuList[i].select = 0;
        this.setData({
          cids: menuList[i].sub_category_id
        })
      }
    }
    this.setData({
      goodsStart: 1,
      endFlage: 1,
      end: 999,
      menuList: menuList
    })
    this.goTopFun();
    this.getGoodsListParameter();
  },
  getmore(){
    var start = this.data.goodsStart;
    var newStart = start + 1;
    this.setData({
      goodsStart: newStart
    })
    console.log(newStart)
    this.getGoodsListParameter();
  },
  //获得参数
  getGoodsListParameter() {
    console.log(this.data.cids)
    console.log(this.data.sortKind)
    var body = {};
    var queryList = [
      "cids:" + this.data.cids,
      "shopId:" + wx.getStorageSync('supermarket_id')
    ];
    var sortList = [
      this.data.sortKind + ":" + this.data.paixu
    ];
    body.queryList = queryList;
    body.rows = this.data.rows;
    body.sortList = sortList;
    body.start = this.data.goodsStart;
    console.log(body)
    this.getGoodsList(body)
  },
  getGoodsList(body) {
    var url = getApp().data.url + 'solr/product/productQuery';
    wx.showLoading({
      title: '',
      icon: "loading"
    })
    if (this.data.goodsStart <this.data.end){
      getApp().wxAjax(this.splitGoodsList, url, body);
    }else{
      wx.hideLoading({});
      if(this.data.endFlage == 1){
        this.setData({
          endFlage:2
        })
        wx.showToast({
          title: '全部加载完成',
          icon: 'none'
        })
      }
    }
   
  },
  splitGoodsList(res) {
    wx.hideLoading({});
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      var goodsListObj = resObj.body;
      var goodsListArr;
      //无分页时直接取
      if(this.data.goodsStart == 1){
        goodsListArr = resObj.body.data;
      }else{
        goodsListArr = this.data.goodsList;
        goodsListArr.push.apply(goodsListArr, resObj.body.data);
      }
      this.setData({
        goodsList: goodsListArr,
        end: goodsListObj.pages
      })
      console.log(goodsListObj)
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  toGoodesDetails(e){
    var skuid = e.currentTarget.dataset.skuid;
    wx.navigateTo({
      url: '../pDetails/pDetails?skuid=' + skuid
    })
  },
  addGoods(e){
    var skuid = e.currentTarget.dataset.skuid;
    this.setData({
      sku: skuid
    })
    getApp().addPro(this);
  },
  bottomNav(e) {
    getApp(e).bottomNav(e);
  },
  toSearch(){
    wx.navigateTo({
      url: '../search/search',
    })
  }
})